var service = require('../../utilService.js')
var manager = require('../../favManager.js')
var noti = require('../../WxNotificationCenter.js')

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: null,
      observer: function (newValue, oldValue) {
        var isFav = true
        var saleText = "可兑换"
        if (parseInt(newValue.store) == 0 || newValue.store == "") {
          isFav = false
        }
        if (newValue.saleText != null && newValue.saleText.length > 0) {
          saleText = newValue.saleText
        }
        newValue.cover = service.configCover(newValue.cover)
        this.setData({ isFav: isFav, favCount: newValue.favorityCount, saleText: saleText })
      }
    },
    anctionType: {
      type: Number,
      value: 0   // 0 竞拍中  1 已经送出
    },
    brand: {
      type: Boolean,
      value: false
    },
    auction: {
      type: String,
      value: false
    }, //是否是竞拍中
    needStatusShow: {
      type: Boolean,
      value: false, 
    },// 是否需要显示 右上角  商品 审核状态
    saleText: {
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    isFav: false,
    favCount: 0,
    cover: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    likeActionBtnClick() {
      this.triggerEvent("likeActionBtnClick", this.properties.data)
    },
    iconClick() {
      this.pushDetail()
      let data = this.properties.data
      this.saveProductName(data.name == null ? data.goodsName : data.name)
      this.triggerEvent("selectedItem", this.properties.data)
    },
    /**
     * 保存商品名 查询更多商品使用，时返回数据定位更加精准
     */
    saveProductName(goodsName){
      try {
        var value = wx.getStorageSync('products')
        var datas = value.split(",");
        if (value) {     
          if (datas.length < 100) {
            console.log(typeof value)
            value = value + "," + goodsName;
            wx.setStorage({
              key: "products",
              data: value
            })
          } else {
            datas.shift();
            datas.join(",");
            datas = datas + "," + goodsName;
            wx.setStorage({
              key: "products",
              data: datas
            })
          }
        }else{
          wx.setStorage({
            key: "products",
            data: goodsName
          })
        }
      } catch (e) {
        // Do something when catch error
      }
    },
    userIconClick() {
      wx.navigateTo({
        url: '/mineSubs/pages/homepage/personalhomepage?userId=' + this.properties.data.publishUserId,
      })
    },
    contentClick() {
      this.pushDetail()
      this.triggerEvent("selectedItem", this.properties.data)
    },
    pushDetail() {
      wx.navigateTo({
        url: "/goods/pages/goodsDetails/goodsDetails?goods_id=" + this.properties.data.id + '&goods_type=' + this.properties.data.saleTypeId
      })
    },
    favClick() {
      if (this.data.isFav == false) {
        this.favRequest()
      } else {
        var that = this
        wx.showModal({
          title: '提示',
          content: '您将取消对此商品的收藏！',
          success: function (res) {
            if (res.confirm) {
              that.deleteFavRequest()
            }
          }
        })

      }
    },
    favRequest() {
      var that = this

      manager.favRequest(this.properties.data.id, 
      function(data) {
        var count = that.data.favCount + 1
        that.setData({ isFav: true, favCount: count })
        that.properties.data.store = 1
      })
    },
    
    deleteFavRequest() {
      var that = this

      manager.deleteFavRequest(this.properties.data.id,
        function (data) {
          var count = that.data.favCount - 1
          that.setData({ isFav: false, favCount: count })
          that.properties.data.store = 0
        })
    }
  }

})
